﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ComputerBeacon.Csv;
using ComputerBeacon.Serialization;

namespace UnitTestProject1 {
    [TestClass]
    public class SerializationTest {


        [TestMethod]
        public void Serialize() {
            var cars = new[]{
                new Car{ Year = 1997, Make = "Ford", Model = "E350", Length = 2.34 },
                new Car{Year = 2000, Make = "Mercury", Model = "Cougar", Length = 2.38 }
            };

            Assert.AreEqual(
                "Year,Make,Model,Length\r\n1997,Ford,E350,2.34\r\n2000,Mercury,Cougar,2.38",
                Serialization.Serialize(cars)
            );
        }

        [TestMethod]
        public void Serialize_Roundtrip() {
            var cars = new[]{
                new Car{ Year = 1997, Make = "Ford", Model = "E350", Length = 2.34 },
                new Car{Year = 2000, Make = "Mercury", Model = "Cougar", Length = 2.38 }
            };

            var cars2 = Serialization.Deserialize<Car>(Serialization.Serialize(cars));
            Assert.AreEqual(cars.Length, cars2.Count);
            for (int i = 0; i < cars.Length; i++) Assert.AreEqual(cars[i], cars2[i]);
        }

        [TestMethod]
        public void Serialize_AlwaysQuote() {
            var cars = new[]{
                new Car{ Year = 1997, Make = "Ford", Model = "E350", Length = 2.34 },
            };

            Assert.AreEqual(
                "\"Year\",\"Make\",\"Model\",\"Length\"\r\n\"1997\",\"Ford\",\"E350\",\"2.34\"",
                Serialization.Serialize(cars, true, Serialization.EnclosureMode.Always)
            );
        }

        [TestMethod]
        public void Serialize_AlwaysQuote_Roundtrip() {
            var cars = new[]{
                new Car{ Year = 1997, Make = "Ford", Model = "E350", Length = 2.34 },
                new Car{Year = 2000, Make = "Mercury", Model = "Cougar", Length = 2.38 }
            };

            var cars2 = Serialization.Deserialize<Car>(
                Serialization.Serialize(cars, true, Serialization.EnclosureMode.Always)
            );
            Assert.AreEqual(cars.Length, cars2.Count);
            for (int i = 0; i < cars.Length; i++) Assert.AreEqual(cars[i], cars2[i]);
        }

        [TestMethod]
        public void SpecialCharRoundtrip() {
            var cars = new[]{
                new CarPrice{ Year = 1997, Make = "Ford", Model = "E350", Description = "ac, abs, moon", Price = 3000},
                new CarPrice{ Year = 1999, Make = "Chevy", Model = "Venture \"Extended Edition\"", Description = "", Price = 4900},
                new CarPrice{ Year = 1999, Make = "Chevy", Model = "Venture \"Extended Edition, Very Large\"", Description="", Price = 5000},
                new CarPrice{ Year = 1996, Make = "Jeep", Model = "Grand Cherokee", Description="MUST SELL!\r\nair, moon roof, loaded", Price = 4799}
            };

            var cars2 = Serialization.Deserialize<CarPrice>(Serialization.Serialize(cars));
            Assert.AreEqual(cars.Length, cars2.Count);
            for (int i = 0; i < cars.Length; i++) Assert.AreEqual(cars[i], cars2[i]);
        }

        [TestMethod]
        public void NullValues() {
            var cars = new[]{
                new Car{ Year = 1997, Make = null, Model = "E350", Length = 2.34 },
                new Car{Year = 2000, Make = "Mercury", Model = "Cougar", Length = 2.38 }
            };

            Assert.AreEqual(
                "Year,Make,Model,Length\r\n1997,,E350,2.34\r\n2000,Mercury,Cougar,2.38",
                Serialization.Serialize(cars)
            );
        }
    }
}
